/**
 * @author zhangdi
 * @version 1.0
 * @date 2023.6.16
 */

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EmailUtil {
    private static final ExecutorService executorService = Executors.newCachedThreadPool();

    public static CompletableFuture<Void> sendEmailAsync(String subject, String message) {
        Long curr = System.currentTimeMillis();
        System.out.println("准备发送"+curr);
        return CompletableFuture.runAsync(() -> {
            // 执行发送邮件的操作，例如调用SMTP客户端发送邮件

            sendEmail(subject, message);
            System.out.println("已发送" + (System.currentTimeMillis() - curr));
        }, executorService);
    }

    public static void sendEmail(String subject, String content) {
        // 邮件发送配置
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.163.com");
        properties.put("mail.smtp.port", "25");

        // 邮件发送凭据
        String username = "zhangd6202@163.com";
        String password = "163mail@mima";

        // 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            // 创建邮件
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("278360773@qq.com"));
            message.setSubject(subject);
            message.setText(content);

            // 发送邮件
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        CompletableFuture<Void> cf = EmailUtil.sendEmailAsync("异步测试222", "邮件内容test222");
        System.out.println("但是不影响");
        cf.thenRun(new Runnable() {
            @Override
            public void run() {
                System.out.println("发送完成"+System.currentTimeMillis());
            }
        });
        cf.join();

//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(10000L);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println("准备已发送");
//                EmailUtil.sendEmail("这里是主题test", "这里是内容test");
//                System.out.println("已发送");
//            }
//        });
//        thread.start();
    }
}

